package me.firstapp.spi.shop.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.shop.JsonProductComment;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.shop.ProductCommentService;

@Service
public class ProductCommentServiceImpl extends BaseServiceImpl implements ProductCommentService {

	@Override
	public ListObject<JsonProductComment> findProductCommentList(Long productId) {
		ListObject<JsonProductComment> result = new ListObject<JsonProductComment>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("productId", productId);
			JSONObject resultObject = api(parameters, "/productComment/findProductCommentList.do", HttpMethod.GET,
					"查询商品评论列表");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONArray jsonArray = resultObject.getJSONArray("items");
				List<JsonProductComment> list = new ArrayList<>();
				if (jsonArray != null) {
					for (int i = 0; i < jsonArray.size(); i++) {
						JSONObject jsonObject = jsonArray.getJSONObject(i);
						if (jsonObject != null) {
							JsonProductComment comment = JSONObject.toJavaObject(jsonObject,
									JsonProductComment.class);
							list.add(comment);
						}
					}
				}
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setItems(list);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonProductComment> addProductComment(String content, String orderNo, Long productId, Long fromMemberId) {
		SingleObject<JsonProductComment> result = new SingleObject<JsonProductComment>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("content", content);

			Map<String, Object> parameters = new HashMap<>();
			parameters.put("orderNo", orderNo);
			parameters.put("productId", productId);
			parameters.put("fromMemberId", fromMemberId);

			JSONObject jsonObject = apiBody(parameters, "/productComment/addProductComment.do", "新增商品评论", params);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JsonProductComment comment = JSONObject.toJavaObject(jsonObject.getJSONObject("object"),
						JsonProductComment.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(comment);
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

}
